//封装图片上传方法
const uploadImage = (url: string, ok_title: string, error_title: string) => {
  return new Promise((resolve, reject) => {
    //第一步：选择手机里的照片
    uni.chooseMedia({
      count: 1,
      mediaType: ["image"],
      sizeType: ["compressed"],
      success(res) {
        console.log("选择的照片：", res);
        //正在上传的提示
        uni.showLoading({
          title: ok_title,
          mask: true,
        });
        //第二步：将选择照片上传
        uni.uploadFile({
          //上传地址，绝对地址
          url,
          //上传路径
          filePath: res.tempFiles[0].tempFilePath,
          //后台接收的文件名
          name: "file",
          //上传请求头
          header: {
            accept: "application/json",
          },
          //上传成功返回
          success(res_img) {
            console.log("上传成功返回：", res_img);
            //成功返回给页面
            resolve(res_img);
            //上传成功，隐藏loading
            uni.hideLoading();
          },
          //失败返回
          fail(error) {
            uni.showToast({
              title: error_title,
              icon: "error",
            });
          },
        });
      },
      fail(error) {
        uni.showToast({
          title: error_title,
          icon: "error",
        });
      },
    });
  });
};

export { uploadImage };
